// JavaScript Document
define(function(require,exports,module){
	var lotTimer,runTime = 3050,awardData,clickTag = 1,$ = require("$");
	
	function getData(dataUrl){
		$.ajax({
			type: "POST",
			dataType:"json",
			url:dataUrl,
			async:false,
			success: function(data){
				awardData = data;
			}
		});
	}
    function setRunTime(awardMoney){
        var isIe = /msie/.test(navigator.userAgent.toLowerCase());
        var isMozilla = /firefox/.test(navigator.userAgent.toLowerCase());
        switch(awardMoney){
            case 0:
                if(isIe){ runTime = 3050;}else if(isMozilla){runTime = 3220;}else{runTime = 3200;}
                break;
            case 5:
                if(isIe){runTime = 3150;}else if(isMozilla){runTime = 3320;}else{runTime = 3300;}
                break;
            case 10:
                if(isIe){runTime = 3250;}else if(isMozilla){runTime = 3420;}else{runTime = 3400;}
                break;
            case 20:
                if(isIe){runTime = 3350;}else if(isMozilla){runTime = 3520;}else{runTime = 3500;}
                break;
            default :
                if(isIe){runTime = 3050;}else if(isMozilla){runTime = 3220;}else{runTime = 3200;}
        }
    }
	function showResult(awardMoney){
		switch(awardMoney){
            case 0:
                $("#awardResult").attr("class","awardResult0").show();
                break;
            case 5:
                $("#awardResult").attr("class","awardResult5").show();
                break;
            case 10:
                $("#awardResult").attr("class","awardResult10").show();
                break;
            case 20:
                $("#awardResult").attr("class","awardResult20").show();
                break;
            default :
                $("#awardResult").attr("class","awardResult0").show();
                break;
        }
	}
	//初始化
	getData("/ernie/ajax.php?action=getusertimes");
	$("#awardTimes>span").text(awardData.times+"次");
	$("a.close").click(function(e) {
        $(this).parent("div").hide();
    });
	
	//开始抽奖
	$("#start").click(function(e) {
		if($(this).hasClass("invalid") || clickTag == 0) return false;
        getData("/ernie/ajax.php?action=getusertimes");
        var lotTimes = awardData.times<=0 ? 0 : awardData.times-1;
        $("#awardTimes>span").text(lotTimes+"次");
		if( awardData.times > 0 ){
			clearInterval(lotTimer);
            clickTag = 0;
			var index = -1;
			lotTimer = setInterval(function(){
				index++;
				if(index>=4){index=0;}
				$("#award>li").eq(index).addClass("select").siblings("li").removeClass("select");
			},100);
            getData("/ernie/ajax.php?action=getresult");
            var money = awardData.money;
            setRunTime(money);
			setTimeout(function(){
				clearInterval(lotTimer);
				showResult(money);
			},runTime);
            setTimeout(function(){clickTag = 1;},4000);
		}else{
			$("#noAward").show();
		}
    });
    //关闭抽奖结果
    $("#awardResult>a.close").click(function(){
        if(awardData.times<=0){
            $("#noAward").show();
        }
    });
	
	//中奖列表
	(function(){
		var awardNum = $(".lottery-list>ul>li").length,scrollTimer;
		function scrollResult(){
			$(".lottery-list>ul").animate({"marginTop":"-25px"},500,function(){
				$(".lottery-list>ul").css({marginTop:0}).find("li:first").appendTo($(".lottery-list>ul"));
			});
		}
		if(awardNum > 11){
			$(".lottery-list").hover(function(){
				clearInterval(scrollTimer);
			},function(){
				scrollTimer = setInterval(function(){
					scrollResult();
				},3000);
			}).trigger("mouseout")
		}
	})();

    (function(){
        if($("#J_LogBtn").length<=0) return;
        $("#J_LogBtn").click(function(){
            $("#J_LogBlk").show();
        });
    })();

    //点击文本框复制其内容到剪贴板上方法
    var isMozilla = /firefox/.test(navigator.userAgent.toLowerCase());

    $("#J-CopyLink").click(function(){
        $("#J-Link").select();
        if(window.clipboardData){
            clipboardData.setData('Text',$("#J-Link").val());
            alert("已复制到剪切板")
        }
    });
});